<template>
  <div>
    <div class="header">
      <div class="title">游戏记录</div>
      <div class="AllDelete">
        <el-button
          size="small"
          type="danger"
          icon="el-icon-delete"
          @click="beginClickBg(), deleteAll()"
          >批量删除</el-button
        >
      </div>
      <el-table
        :data="tableDate"
        stripe
        style="width: 100%; margin: 0 auto"
        header-cell-class-name="active-header"
        cell-class-name="table-center"
        @select-all="selectHandle"
        @select="selectHandle">
        <el-table-column type="selection" width="30"></el-table-column>
        <el-table-column prop="username" label="昵称"></el-table-column>
        <el-table-column prop="gridType" label="网格类型"></el-table-column>
        <el-table-column prop="gameLevel" label="游戏难度"> </el-table-column>
        <el-table-column prop="time" label="用时"> </el-table-column>
        <el-table-column prop="date" label="日期" width="200"></el-table-column>
        <el-table-column prop="complete" label="完成率"></el-table-column>
        <el-table-column prop="isSuccess" label="是否通过" width="150">
          <template slot-scope="scope">
            <el-tag :type="scope.row.isSuccess ? 'success' : 'danger'" disable-transitions>{{
              scope.row.isSuccess ? "通过" : "未通过"
            }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="danger"
              @click="beginClickBg(), handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <Pagination :total="total" :pageSize="pageSize" @CurrentChange="CurrentChange"></Pagination>
      </div>
    </div>
    <div class="content">
      <div class="title">游戏总览</div>
      <div class="pic-box">
        <div id="linear" class="pic linear"></div>
        <div id="pie" class="pic pie"></div>
      </div>
    </div>
  </div>
</template>

<script>
import * as echarts from "echarts";
import Pagination from "@/views/pc/record/Pagination.vue";
import { mapState, mapMutations } from "vuex";
export default {
  components: { Pagination },
  data() {
    return {
      tableDate: [],
      total: 1,
      pageSize: 8,
      current: 1,
      ids: [],
      linearData: [],
      pieData: [],
    };
  },
  methods: {
    ...mapMutations("record", ["setRecord"]),
    ...mapMutations("audio", ["beginClickBg"]),
    CurrentChange(val) {
      this.current = val;
      this.getTableDate();
    },
    getTableDate() {
      this.total = this.record.length;
      this.tableDate = this.record.filter((item, index) => {
        return index >= this.pageSize * (this.current - 1) && index < this.pageSize * this.current;
      });
    },
    selectHandle(selection) {
      this.beginClickBg();
      let arr = [];
      selection.forEach((ele) => {
        arr.push(ele.id);
      });
      this.ids = arr;
    },
    deleteAll() {
      if (this.ids.length === 0) return;
      this.$confirm("此操作将永久删除该记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.beginClickBg();
          let arr = JSON.parse(JSON.stringify(this.record));
          this.ids.forEach((ele) => {
            arr = arr.filter((item) => {
              return ele !== item.id;
            });
          });
          this.setRecord(arr);
          this.getTableDate();
        })
        .catch(() => {
          this.beginClickBg();
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleDelete(index, row) {
      this.$confirm("此操作将永久删除该记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.beginClickBg();
          let arr = this.record.filter((item) => {
            return row.id !== item.id;
          });
          this.setRecord(arr);
          this.getTableDate();
        })
        .catch(() => {
          this.beginClickBg();
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    getLinearData() {
      let arr = [0, 0, 0, 0, 0];
      this.record.forEach((ele) => {
        if (ele.isSuccess) {
          let i = Number(ele.gridType.substr(0, 2));
          arr[i / 5 - 2]++;
        }
      });
      this.linearData = arr;
    },
    initLinear() {
      let myChart = echarts.init(document.getElementById("linear"));
      myChart.setOption({
        title: {
          text: "通关次数",
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
        },
        xAxis: {
          data: ["10 x 10", "15 x 15", "20 x 20", "25 x 25", "30 x 30"],
        },
        yAxis: {},
        series: [
          {
            name: "通关次数",
            type: "bar",
            data: this.linearData,
          },
        ],
      });
    },
    getPieData() {
      let arr = [
        { value: 0, name: "10 x 10" },
        { value: 0, name: "15 x 15" },
        { value: 0, name: "20 x 20" },
        { value: 0, name: "25 x 25" },
        { value: 0, name: "30 x 30" },
      ];
      this.record.forEach((ele) => {
        if (ele.isSuccess) {
          let i = Number(ele.gridType.substr(0, 2));
          arr[i / 5 - 2].value++;
        }
      });
      this.pieData = arr;
    },
    initPie() {
      let myChart = echarts.init(document.getElementById("pie"));

      myChart.setOption({
        tooltip: {
          trigger: "item",
        },
        legend: {
          top: "5%",
          left: "center",
        },
        series: [
          {
            name: "Access From",
            type: "pie",
            radius: ["40%", "70%"],
            avoidLabelOverlap: false,
            itemStyle: {
              borderRadius: 10,
              borderColor: "#fff",
              borderWidth: 2,
            },
            label: {
              show: false,
              position: "center",
            },
            emphasis: {
              label: {
                show: true,
                fontSize: 40,
                fontWeight: "bold",
              },
            },
            labelLine: {
              show: false,
            },
            data: this.pieData,
          },
        ],
      });
    },
  },
  mounted() {
    this.getTableDate();
    this.getLinearData();
    this.initLinear();
    this.getPieData();
    this.initPie();
  },
  computed: {
    ...mapState("record", ["record"]),
  },
};
</script>

<style lang="less" scoped>
.header {
  position: relative;
  width: 100%;
  margin-bottom: 20px;
  background-color: #fff;

  .title {
    height: 40px;
    margin: 0 auto;
    margin-bottom: 20px;
    line-height: 40px;
    text-align: center;
    font-size: 18px;
    color: #409eff;
  }

  .AllDelete {
    position: absolute;
    top: 10px;
    left: 20px;
  }

  /deep/ .el-table__row {
    height: 20px;
  }

  /deep/ .el-table__cell {
    padding: 8px 0;
  }

  /deep/ .active-header {
    color: #333;
    text-align: center;
  }
  /deep/ .table-center {
    text-align: center;
  }

  .pagination {
    padding: 10px;
  }
}

.content {
  width: 100%;
  padding: 20px 0;
  background-color: #fff;

  .title {
    height: 40px;
    margin: 0 auto;
    margin-bottom: 30px;
    line-height: 40px;
    text-align: center;
    font-size: 18px;
    color: #409eff;
  }

  .pic-box {
    display: flex;
    justify-content: space-around;
    width: 100%;

    .pic {
      height: 300px;
      padding: 10px;
      box-shadow: 1px 1px 10px #aaa;
    }

    .linear {
      width: 55%;
    }

    .pie {
      width: 35%;
    }
  }
}
</style>
